﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using deviceManage.Models;
using deviceManage.vo;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using deviceManage.Filters;
using deviceManage.Tools;
using deviceManage.VO;
using deviceManage.Service;
using System.IO;
using OfficeOpenXml;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml.Drawing;
using System.Text;
using System.Globalization;

namespace deviceManage.Controllers
{
    /// <summary>
    /// 设备变更控制器
    /// </summary>
    [CheckLoginAttribute]
    public class DeviceChangeController : Controller
    {
        private readonly DeviceCtx _context;
        private DeviceService deviceService;
        private IHostingEnvironment _hostingEnvironment;

        public DeviceChangeController(DeviceCtx context, IHostingEnvironment hostingEnvironment)
        {
            _context = context;
            _hostingEnvironment = hostingEnvironment;
            deviceService = new DeviceService(_context);
        }

        public IActionResult Index()
        {
            //获取设备分类，用于下拉显示
            var typeList = _context.DeviceType.Where(p => p.Deleted == 0).OrderBy(p => p.SortOrder).ToList();
            //是否只读权限
            int? flag = HttpContext.Session.GetInt32("viewFlag");

            return View(Tuple.Create(typeList,flag));
        }


        //ajax 获取分页数据,在建项目
        public AjaxResult List(int page, int limit, String name)
        {
            AjaxResult r = new AjaxResult();
            IQueryable<Device> query = _context.Device;
            if (!string.IsNullOrWhiteSpace(name))
            {
                query = query.Where(p => p.Name.Contains(name));
            }

            if (!string.IsNullOrWhiteSpace(Request.Form["deviceTypeId"]))
            {
                query = query.Where(p => p.DeviceTypeId == Request.Form["deviceTypeId"]);
            }

            if (!string.IsNullOrWhiteSpace(Request.Form["deviceId"]))
            {
                query = query.Where(p => p.DeviceId == Request.Form["deviceId"]);
            }
            if (!string.IsNullOrWhiteSpace(Request.Form["start"]))
            {
                DateTime start = DateTime.Parse(Request.Form["start"]);
                query = query.Where(p => p.BuyDate >= start);
            }
            if (!string.IsNullOrWhiteSpace(Request.Form["end"]))
            {
                DateTime end = DateTime.Parse(Request.Form["end"]);
                query = query.Where(p => p.BuyDate <= end);
            }
            if (!string.IsNullOrWhiteSpace(Request.Form["no"]))
            {
                query = query.Where(p => p.No == Request.Form["no"]);
            }

            r.Count = query.Count();
            r.Code = 0;
            try
            {
                r.Data = query.Include(p => p.DeviceType).Include(p => p.Room)
                .OrderBy(p => p.Status)
                .OrderBy(p => p.Name)
                .Skip((page - 1) * limit).Take(limit)
                .ToList();
            }
            catch (Exception e)
            {
                r.Msg = e.Message;
            }
            return r;
        }


        /// <summary>
        /// 设备转出
        /// </summary>
        /// <returns></returns>
        public IActionResult MovieOut(string id)
        {
            Device device = this.deviceService.GetById(id);
            var departList = _context.Depart.Where(p => p.Deleted == 0).OrderBy(p => p.SortOrder).ToList();
            return View(Tuple.Create(device,departList));
        }

        /// <summary>
        /// 添加转出日志，更新设备状态
        /// </summary>
        /// <returns></returns>
        public AjaxResult AddMoveOutLog(MoveoutLog log)
        {
            //更改设备状态
            Device device = this.deviceService.GetById(log.DeviceId);
            device.Status = DeviceState.STATE_TRANS_OUT;
            this.deviceService.updateDevice(device);


            //添加转出日志
            _context.MoveoutLog.Add(log);
            _context.SaveChanges();


            return AjaxResult.GetOK("转出成功");
        }


        /// <summary>
        /// 设备移交到某个部门
        /// </summary>
        /// <returns></returns>
        public IActionResult TransferDepart(string id)
        {
            Device device = this.deviceService.GetById(id);
            var departList = _context.Depart.Where(p => p.Deleted == 0).OrderBy(p => p.SortOrder).ToList();
            return View(Tuple.Create(device, departList));
        }

        /// <summary>
        /// 添加设备移交日志
        /// </summary>
        /// <returns></returns>
        public AjaxResult AddTransferLog(TransferLog log)
        {
            //更新部门
            Device device = this.deviceService.GetById(log.DeviceId);
            device.DepartId = log.DepartId;
            this.deviceService.updateDevice(device);

            //添加转出日志
            _context.TransferLog.Add(log);
            _context.SaveChanges(); 
            return AjaxResult.GetOK("移交成功");
        }
    }     
}